namespace Dida.Dxml
{
	public class WorldXmlCompiler : XmlCompiler
	{
		World world;

		AniSetCompiler rootAniSetCompiler;		

		public WorldXmlCompiler(World world)
		{
			this.world = world;
			ObjectConstructed += OnObjectConstructed;
			CompilationFinished += OnCompilationFinished;

			rootAniSetCompiler = new AniSetCompiler();
			AddGlobalContainer(rootAniSetCompiler);
		}

		private void OnObjectConstructed(object sender, ObjectConstructedArgs args)
		{
			object obj = args.Object;
			if (obj is Actor)
			{
				world.AddActor((Actor)obj);
			}
		}

		private void OnCompilationFinished(object sender, CompilationFinishedArgs args)
		{
			AniSet rootAniSet = (AniSet)GetGlobalContainerObj(rootAniSetCompiler);
			world.RootAniSets.Add(rootAniSet);

			MapInfo mapInfo = (MapInfo)args.ObjectManager.GetObject(
				"mapInfo", typeof(Dida.MapInfo));
			world.StaticCollision = mapInfo.CollisionWorld.CollisionTree;
		}
	}
}
